<template>
	<view class="goods-item">
		<!-- 左侧盒子 -->
		<view class="goods-item-lefe">
			<radio :checked="goods.goods_state" color="#C00000" v-if="showRedio" @click="radioClickHandler"></radio>
			<image :src="goods.goods_small_logo || defaultPic" class="goods-pic"></image>
		</view>
		<!-- 右侧盒子 -->
		<view class="goods-item-right">
			<view class="goods-name">{{goods.goods_name}}</view>
			<view class="goosd-info-box">
				<!-- 商品价格 -->
				<view class="goods-price">￥{{goods.goods_price +'.00'}}</view>
				<!-- 商品数量 -->
				<uni-number-box :min="1" :value="goods.goods_count" v-if="showNum" @change="numberChangeHandler"></uni-number-box>
			</view>
		</view>
	</view>
</template>
<script>
	export default {
		//接收数据
		props: {
			goods: {
				type: Object,
				default: {}
			},
			// 是否展示图片左侧的 radio
			showRedio: {
				type: Boolean,
				//默认情况下不会展示radio组件
				default: false
			},
			showNum:{
				type: Boolean,
				//默认情况下不会展示Numbox组件
				default: false
			}
		},
		data() {
			return {
				//给失效图片一个默认的图片
				defaultPic: 'https://img3.doubanio.com/f/movie/8dd0c794499fe925ae2ae89ee30cd225750457b4/pics/movie/celebrity-default-medium.png'

			}
		},
		methods: {
			//radio点击事件
			radioClickHandler() {
				// 通过 this.$emit() 触发外界通过 @ 绑定的 radio-change 事件，
				// 同时把商品的 Id 和 勾选状态 作为参数传递给 radio-change 事件处理函数
				this.$emit('radio-change', {
					goods_id: this.goods.goods_id,
					goods_state: !this.goods.goods_state
				})
			},
			//监听到了numberbox组件数量变化的事件
			numberChangeHandler(val){
				// console.log(val);
				this.$emit('num-change',{
					goods_id:this.goods.goods_id,
					goods_count:+val,
				})
			}
		}
		// filters:{
		// 	tofixed(num){
		// 		return Number(num).toFixed(2)
		// 	}
		// }
	}
</script>
<style lang="scss">
	.goods-item {
		display: flex;
		padding: 10px 5px;
		border-bottom: 1px solid white;

		.goods-item-lefe {
			margin-right: 5px;
			display: flex;
			justify-content: space-between;
			align-items: center;

			.goods-pic {
				width: 100px;
				height: 100px;
				display: block;
			}
		}

		.goods-item-right {
			/* //水平对齐 */
			display: flex;
			flex: 1;
			/* //纵向布局 */
			flex-direction: column;
			/* //贴边对齐 */
			justify-content: space-between;

			.goods-name {
				font-size: 13px
			}

			.goosd-info-box {
				display: flex;
				justify-content: space-between;
				align-items: center;
				.goods-price {
					color: #C00000;
					font-size: 16px
				}
			}
		}
	}
</style>